package com.cavas.domain.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cavas.domain.bean.Abscence;
import com.cavas.domain.bean.Membre;

@Repository
public class DaoAbscenceImpl implements DaoAbscence{

	
	@Autowired
	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory session){
		this.sessionFactory = session;
	}
	
	@Override
	public void addAbscence(Abscence rt, Membre membre) {
		Session session = sessionFactory.getCurrentSession();
		session.save(rt);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Abscence> allAbscence() {
		return sessionFactory.getCurrentSession().createQuery("from Abscence").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Abscence> getAbscenceMembre(String nom) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Abscence clt where clt.nom = :enom");
		query.setParameter("enom", nom);
		return  query.list();
	}

	@Override
	public void deleteAbscence(int id) {
		Session session = sessionFactory.getCurrentSession();
		Abscence rt = (Abscence) session.get(Abscence.class, id);
		if(rt != null)
			session.delete(rt);
		
	}

	@Override
	public int TotalAbscence() {
		List<Abscence> list = this.allAbscence();
		int total = 0;
		
		for(Abscence r : list){
			total = total + r.getMontant();
		}
		return total;
	}

}
